﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Text;
using Silk.NET.OpenGLES;
using TTEngine.Resource;


namespace TTEngine.Graphics
{
    public sealed class TextureDepth : ResourceObj, ITexture
    {
        protected override void OnDestory()
        {
            if (TexID != 0)
                TTEngine.Runtime.activeGL.DeleteTexture(TexID);
            TexID = 0;
        }
        public TextureDepth(Resource.ResourcePackage package, uint width, uint height)
            : base(package)
        {
            this.TexID = TTEngine.Runtime.activeGL.GenTexture();
            TTEngine.Runtime.activeGL.BindTexture(TextureTarget.Texture2D, this.TexID);

            TTEngine.Runtime.activeGL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); // GL_NEAREST
            TTEngine.Runtime.activeGL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest); // GL_NEAREST
            TTEngine.Runtime.activeGL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); // GL_CLAMP_TO_EDGE
            TTEngine.Runtime.activeGL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); // GL_CLAMP_TO_EDGE

            this.Width = width;
            this.Height = height;
            unsafe
            {
                InternalFormat glformat = InternalFormat.Depth24Stencil8;
                PixelFormat pformat = PixelFormat.DepthStencil;
                PixelType ptype = PixelType.UnsignedInt248Oes;
                TTEngine.Runtime.activeGL.TexImage2D(TextureTarget.Texture2D, 0, glformat, (uint)width, (uint)height, 0, pformat, ptype, null);
            }
            this.format = TextureType.Depth24_8;
        }
        public uint TexID
        {
            get;
            private set;
        }

        public uint Width
        {
            get;
            private set;
        }
        public uint Height
        {
            get;
            private set;
        }
        public TextureType format
        {
            get;
            private set;
        }
        public bool IsRenderTarget() => false;
        public bool IsArray() => false;


        public bool MipMap
        {
            get;
            private set;
        }

    }
    public sealed class Texture2D : ResourceObj, ITexture
    {


        override protected void OnDestory()
        {
            if (this.TexID > 0)
            {
                TTEngine.Runtime.activeGL.DeleteTexture(this.TexID);
                this.TexID = 0;
            }

        }
        bool _Linear = false;
        public bool Linear
        {
            get
            {
                return _Linear; ;
            }
            set
            {
                TTEngine.Runtime.activeGL.BindTexture(TextureTarget.Texture2D, this.TexID);
                if (value)
                {
                    TTEngine.Runtime.activeGL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
                    TTEngine.Runtime.activeGL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
                }
                else
                {
                    TTEngine.Runtime.activeGL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
                    TTEngine.Runtime.activeGL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMinFilter.Nearest);

                }
                _Linear = value;
            }
        }
        //Resource 构造函数规范
        //第一个参数必须是ResourcePackage
        public Texture2D(Resource.ResourcePackage package, uint width, uint height, TextureType format, byte[] data = null, bool mipmap = false)
            : base(package)
        {

            this.Width = width;
            this.Height = height;
            this.format = format;


            this.TexID = TTEngine.Runtime.activeGL.GenTexture();

            TTEngine.Runtime.activeGL.BindTexture(TextureTarget.Texture2D, this.TexID);
            //gl.PixelStoreI()
            //this._webgl.pixelStorei(this._webgl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
            //this._webgl.pixelStorei(this._webgl.UNPACK_FLIP_Y_WEBGL, 0);

            {
                TTEngine.Runtime.activeGL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
                TTEngine.Runtime.activeGL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMinFilter.Nearest);
            }
            TTEngine.Runtime.activeGL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
            TTEngine.Runtime.activeGL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);



            bool gray = format <= TextureType.R_Float;
            InternalFormat glformat = gray ? InternalFormat.Red : InternalFormat.Rgba;
            PixelFormat pformat = gray ? PixelFormat.Red : PixelFormat.Rgba;
            PixelType ptype = PixelType.UnsignedByte;
            if (format == TextureType.RGBA_Float || format == TextureType.R_Float)
                ptype = PixelType.Float;
            unsafe
            {
                fixed (void* ptr = data)
                {
                    TTEngine.Runtime.activeGL.TexImage2D(TextureTarget.Texture2D, 0, glformat, (uint)width, (uint)height, 0, pformat, ptype, ptr);
                }
            }

            this._mipmap = mipmap;
            if (mipmap)
                TTEngine.Runtime.activeGL.GenerateMipmap(TextureTarget.Texture2D);
        }


        public void UploadImageData(int x, int y, uint w, uint h, IImage data)
        {
            if (data.Width != this.Width || data.Height != this.Height)
                throw new Exception("error size.");
            if (data.Type == ImageFormat.Gray && this.format != TextureType.R_U8)
                throw new Exception("error format");
            if (data.Type == ImageFormat.RGBA && this.format != TextureType.RGBA_32)
                throw new Exception("error format");
            UploadImageData(x, y, w, h, data.GetData());
        }
        public void UploadImageData<T>(int x, int y, uint w, uint h, T[] data) where T : unmanaged
        {


            TTEngine.Runtime.activeGL.BindTexture(TextureTarget.Texture2D, this.TexID);
            bool gray = format <= TextureType.R_Float;
            InternalFormat glformat = gray ? InternalFormat.Red : InternalFormat.Rgba;
            PixelFormat pformat = gray ? PixelFormat.Red : PixelFormat.Rgba;
            PixelType ptype = PixelType.UnsignedByte;
            if (format == TextureType.RGBA_Float || format == TextureType.R_Float)
                ptype = PixelType.Float;
            unsafe
            {
                fixed (void* ptr = data)
                {
                    TTEngine.Runtime.activeGL.TexSubImage2D(TextureTarget.Texture2D, 0, x, y, w, h, pformat, ptype, ptr);
                }
            }

            if (this.MipMap)
            {
                TTEngine.Runtime.activeGL.GenerateMipmap(TextureTarget.Texture2D);
            }
        }
        ////这个要放到平台实现中，每个平台可以有不同
        //public void UploadImage(IImage image)
        //{
        //    if (image.Width != this.Width || image.Height != this.Height)
        //        throw new Exception("error size.");
        //    UploadImageData(0, 0, image.Width, image.Height, image.GetImgData());
        //}
        public uint TexID
        {
            get;
            private set;
        }
        bool _mipmap = false;
        public bool MipMap
        {
            get
            {
                return _mipmap;
            }

        }

        public uint Width
        {
            get;
            private set;
        }

        public uint Height
        {
            get;
            private set;
        }
        public TextureType format
        {
            get;
            private set;
        }



        public bool IsArray()
        {
            return false;
        }

        public bool IsRenderTarget()
        {
            return false;
        }
        //释放资源

    }
}
